package com.example.minzhengtong.ui

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.example.minzhengtong.R
import com.example.minzhengtong.base.BaseFg
import com.example.minzhengtong.databinding.FragmentThirdBinding
import com.example.minzhengtong.utils.LogUtil
import com.example.minzhengtong.utils.WUtils
import com.gyf.immersionbar.ImmersionBar
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * created by WY on 2021/5/27.
 */
class ThirdFG : BaseFg() {

    companion object {
        @JvmStatic
        fun newInstance(param1: String = "", param2: String = "") =
            ThirdFG().apply {
                arguments = Bundle().apply {
                }
            }
    }

    private lateinit var binding : FragmentThirdBinding

    private val mTitle = mutableListOf("推荐","省政府","政策","互动","福州","闽政通")
    private val mFragments = mutableListOf(TChildFG.newInstance(),TChildFG.newInstance(),
        TChildFG.newInstance(),TChildFG.newInstance(),TChildFG.newInstance(),TChildFG.newInstance())

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentThirdBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(false).titleBar(binding.titleBar).init()
        val navigator = CommonNavigator(baseContext)
        val navAdapter : CommonNavigatorAdapter = object : CommonNavigatorAdapter(){
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                val titleView = SimplePagerTitleView(baseContext)
                titleView.normalColor = resources.getColor(R.color.textColor666)
                titleView.selectedColor = resources.getColor(R.color.red)
                titleView.paint.isFakeBoldText = index == binding.viewPager.currentItem//选中加粗
                titleView.text = mTitle[index]
                titleView.setOnClickListener {
                    binding.viewPager.currentItem = index
                }
                titleView.textSize = 15f
                titleView.setPadding(0,0,0,0)
                titleView.width = WUtils.getScreenWidth(baseContext) / 6
                titleView.gravity = Gravity.CENTER
                return titleView
            }

            override fun getCount(): Int {
                return mTitle.size
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                val indicator = LinePagerIndicator(baseContext)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineWidth = 52f
                indicator.lineHeight = 7f
                indicator.setColors(resources.getColor(R.color.red))
                indicator.roundRadius = 2f
                return indicator
            }
        }
        binding.viewPager.apply {
            offscreenPageLimit = mTitle.size
            adapter = object : FragmentPagerAdapter(childFragmentManager){
                override fun getCount(): Int {
                    return mTitle.size
                }

                override fun getItem(position: Int): Fragment {
                    return mFragments[position]
                }

                override fun getPageTitle(position: Int): CharSequence? {
                    return mTitle[position]
                }
            }
            addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    navAdapter.getTitleView(baseContext,position).onSelected(position,mTitle.size)
                    navAdapter.notifyDataSetChanged()
                }

                override fun onPageScrollStateChanged(position: Int) {
                }
            })
        }
        navigator.adapter = navAdapter
        binding.indicatorContainer.navigator = navigator
        ViewPagerHelper.bind(binding.indicatorContainer,binding.viewPager)
    }

    override fun initListener() {

    }

    override fun lazyLoad() {

    }
}